package com.yun.servlet;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yun.bean.YunPlayer;
import com.yun.service.PlayerService;
import com.yun.service.impl.PlayerServiceImpl;
import com.yun.util.Page;


@WebServlet("/PlayerServlet/*")
public class PlayerServlet extends HttpServlet {
	private PlayerService ps = new PlayerServiceImpl();
	
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String pathInfo = request.getPathInfo().substring(1);
		if("listAll".equals(pathInfo)) {
			listAll(request,response);
		} else if ("addPlayer".equals(pathInfo)) {
			addPlayer(request,response);
		} else if ("removeById".equals(pathInfo)) {
			removeById(request,response);
		} else if ("qurryById".equals(pathInfo)) {
			qurryById(request,response);
		} else if ("updPlayer".equals(pathInfo)) {
			updPlayer(request,response);
		} else if("listAllByPage".equals(pathInfo)) {
			listAllByPage(request,response);
		}
	}
	private void listAllByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<YunPlayer> plist = ps.listAllByPage(pageNum, Page.pageSize);
		request.setAttribute("plist", plist);
		int total = ps.getTotal();
		int pageCount =(int)Math.ceil(total*1.0/Page.pageSize);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageNum", pageNum);
		request.getRequestDispatcher("/back/player/player_list.jsp").forward(request, response);
	}
	private void updPlayer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id = request.getParameter("p_id");
		String p_name=request.getParameter("p_name");
		String p_car=request.getParameter("p_car");
		YunPlayer yunPlayer=new YunPlayer();
		yunPlayer.setP_id(p_id);
		yunPlayer.setP_name(p_name);
		yunPlayer.setP_car(p_car);
		ps.updPlayer(yunPlayer);
		request.getRequestDispatcher("/PlayerServlet/listAllByPage?pageNum=1").forward(request, response);
	}
	private void qurryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id =request.getParameter("p_id");
		YunPlayer yunPlayer=ps.selectById(p_id);
		request.setAttribute("yunPlayer", yunPlayer);
		request.getRequestDispatcher("/back/player/player_update.jsp").forward(request, response);
	}
	private void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id=request.getParameter("p_id");
		Boolean b=ps.removeById(p_id);
		if (b) {
			request.getRequestDispatcher("/PlayerServlet/listAllByPage?pageNum=1").forward(request, response);
		}
	}
	private void addPlayer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p_id = UUID.randomUUID().toString();
		String p_name=request.getParameter("p_name");
		String p_car=request.getParameter("p_car");
		YunPlayer yunPlayer=new YunPlayer();
		yunPlayer.setP_id(p_id);
		yunPlayer.setP_name(p_name);
		yunPlayer.setP_car(p_car);
		ps.addPlayer(yunPlayer);
		request.getRequestDispatcher("/back/player/player_add.jsp").forward(request, response);
	}
	private void listAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<YunPlayer> plist = ps.listAll();
		request.setAttribute("plist", plist);
		request.getRequestDispatcher("/back/player/player_list.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
